package com.springboot.sba.D2_HttpRequest.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;


@RestController

public class D2_HttpRequest {
    public static void main(String[] args) {
        //System.out.println("/test/y/fs".matches("^(https://|http://|/)[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"));

        //String sss = "2021-10-10 00:00:00".replaceAll("(?:-|:| )", "");

        String s = "ab";
        String s1="a"+"b";

        System.out.println(s==s1);
        System.out.println(s.intern()==s1);
    }


    @Value("${yzm.zzz}")
    private String test;

    @GetMapping("/test")
    public String test() {
        return "fdsfsdf";
    }


    @GetMapping("/download")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        String fileName = "测试.docx";// 文件名
        if (fileName != null) {
            //设置文件路径
            // File file = new File("/home/1.docx");

            File file = new File("D:\\", fileName);
            if (file.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    // return "下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally { // 做关闭操作
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        //return "下载失败";
    }


    @RequestMapping(path = "/getinfo2/{name}/{age}")
    public String getinfo2(@PathVariable("name") String name, @PathVariable("age") String age) {
        return " i am " + name;
    }

    @RequestMapping("/getinfo3")
    public String getinfo3(@RequestParam(value = "name", required = true) String name) {
        String ss = "123";
        System.out.println("6");
        System.out.println(name);
        System.out.println("2");
        return ss;
    }

    @RequestMapping("/getinfo4")
    public String getinfo4(@RequestParam(value = "name", required = false) String name) {
        String ss = "123" + "111";
        System.out.println("1");
        System.out.println(name);
        System.out.println("2");

        return ss;
    }

    @RequestMapping("/getinfo5")
    public String getinfo5(@RequestParam(value = "name", required = true, defaultValue = "111") String name) {
        String ss = "123";
        System.out.println("1");
        System.out.println(name);
        System.out.println("2");

        return "";
    }
}
